package cn.wing.event;

import cn.wing.constant.GuiConstant;
import cn.wing.saveddata.TransactionInfoData;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.PacketDistributor;

import static cn.wing.PlayTradingMain.TRANSACTION_INFO_MAP_CHANNEL;

@Mod.EventBusSubscriber
public class EntityJoin {

    @SubscribeEvent
    public static void onPlayerJoin(EntityJoinLevelEvent event) {
        if (!event.getLevel().isClientSide() && event.getEntity() instanceof ServerPlayer player) {
            TransactionInfoData.get(event.getLevel());
            TRANSACTION_INFO_MAP_CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), new TransactionInfoMapSync(null, null).setClear(true));
            GuiConstant.TRANSACTION_INFO_MAP.forEach((k,v) -> TRANSACTION_INFO_MAP_CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), new TransactionInfoMapSync(k, v)));
        }
    }

}
